package net.xiaoyu233.mitemod.miteite.trans.render;


import net.minecraft.*;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

@Mixin(beh.class)
public abstract class EffectRendererTrans {
    @Shadow protected World a;


    @Shadow public abstract void a(beg par1EntityFX);

    public void addEntityblood(int x, int y, int z, int block_id, int metadata, Entity entity) {
        if (block_id == 0 || entity == null) return;

        // 获取实体的边界框
        AxisAlignedBB entityBoundingBox = entity.boundingBox;
        if (entityBoundingBox == null) return;

        // 计算实体的最小和最大坐标
        double minX = entityBoundingBox.minX;
        double maxX = entityBoundingBox.maxX;
        double minY = entityBoundingBox.minY;
        double maxY = entityBoundingBox.maxY;
        double minZ = entityBoundingBox.minZ;
        double maxZ = entityBoundingBox.maxZ;

        // 计算实体中心点
        double centerX = (minX + maxX) / 2.0;
        double centerY = (minY + maxY) / 2.0;
        double centerZ = (minZ + maxZ) / 2.0;

        // 计算实体尺寸
        double sizeX = maxX - minX;
        double sizeY = maxY - minY;
        double sizeZ = maxZ - minZ;

        // 确定粒子数量（基于实体大小）
        int numParticles = (int) (sizeX * sizeY * sizeZ * 4);
        numParticles = MathHelper.clamp_int(numParticles, 10, 50); // 限制粒子数量

        Block block = Block.getBlock(block_id);
        long visible_on_tick = this.a.getTotalWorldTime() + 1L;

        // 生成血液粒子
        for (int i = 0; i < numParticles; i++) {
            // 在实体边界框内随机生成粒子位置
            double particleX = minX + this.a.rand.nextDouble() * sizeX;
            double particleY = minY + this.a.rand.nextDouble() * sizeY;
            double particleZ = minZ + this.a.rand.nextDouble() * sizeZ;

            // 计算从实体中心向外的运动方向
            double motionX = (particleX - centerX) * 0.1;
            double motionY = (particleY - centerY) * 0.1;
            double motionZ = (particleZ - centerZ) * 0.1;

            // 添加一些随机性
            motionX += (this.a.rand.nextDouble() - 0.5) * 0.02;
            motionY += (this.a.rand.nextDouble() - 0.5) * 0.02;
            motionZ += (this.a.rand.nextDouble() - 0.5) * 0.02;

            // 创建粒子效果
            beg fx = (new bes(this.a, particleX, particleY, particleZ, motionX, motionY, motionZ, block, metadata))
                    .a((int)particleX, (int)particleY, (int)particleZ)
                    .setVisibleOnTick(visible_on_tick);

            this.a(fx);
        }
    }
}
